package com.yzr.util.converter;

import cn.hutool.core.io.FileUtil;
import com.sun.star.ucb.MissingInputStreamException;
import com.yzr.exception.ConversionException;
import com.yzr.util.ConverterUtil;
import com.yzr.util.FileOfType;
import com.yzr.util.FileType;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @description
 * @author: yzr
 * @date: 2021/11/1 17:33
 **/
@Component
public class TextConversion implements Conversion {

    @Resource
    private ConverterUtil converterUtil;

    @Override
    public String convert(String filePath, String target) {
        // 文本
        File file = FileUtil.file(filePath);

        if (!FileType.isText(FileOfType.getType(file))) {
            throw new ConversionException("文本转换为HTML失败,文件不是文本文件");
        }

        return converterUtil.converterHtml(filePath, target).getAbsolutePath();
    }

    @Override
    public boolean convert(InputStream in, OutputStream out) throws MissingInputStreamException {
        if (null == in) throw new MissingInputStreamException("输入流不能为空");

        return converterUtil.converterHtml(in, out);
    }
}
